package com.qianf.qfoffice.controller;
import com.github.pagehelper.PageInfo;
import com.qianf.qfoffice.entity.Question;
import com.qianf.qfoffice.entity.ResponseData;
import com.qianf.qfoffice.entity.User;
import com.qianf.qfoffice.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/OaNew")

/**
 * 资源相关控制器
 */
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    /**
     * 添加笔记
     * @param session
     * @param question
     * @return
     * @throws
     */
    @RequestMapping("/addNote.do")
    @ResponseBody
    public ResponseData addNote( HttpSession session,Question question)  {

        ResponseData<Object> responseData = new ResponseData<>();

        User user = (User) session.getAttribute("user");
        String u_no = user.getNo();

        question.setU_no(u_no);
        question.setQtype("笔记");
        question.setFlag("笔记");
        boolean result = resourceService.addNote(question);

        if (result==true){
            responseData.setData(question);
            responseData.setMsg("添加笔记成功");
        }else{
            responseData.setMsg("添加出错");
        }
        return responseData;

    }


    /**
     * 新增代码
     * @param session
     * @param question
     * @return
     */
    @RequestMapping("/addCode.do")
    @ResponseBody
    public ResponseData addCode(HttpSession session,Question question)  {
        ResponseData<Object> responseData = new ResponseData<>();

        User user = (User) session.getAttribute("user");
        String no = user.getNo();

        question.setU_no(no);
        question.setQtype("代码");
        question.setFlag("代码");

        boolean result = resourceService.addCode(question);
        if(result==true){
            responseData.setData(question);
            responseData.setMsg("添加代码成功");
        }else{
            responseData.setMsg("添加失败");
        }

        return responseData;
    }

    /**
     * 新增面试题
     * @param session
     * @param question1
     * @return
     * @throws
     */
    @RequestMapping("/savequestion.do")
    @ResponseBody
    public ResponseData saveQuestion(HttpSession session, Question question1)  {
        ResponseData<Object> responseData = new ResponseData<>();

        User user = (User) session.getAttribute("user");

        //前端获取参数
        String no = user.getNo();

        //设置到数据库
        question1.setFlag("资源");
        question1.setU_no(no);

        boolean result = resourceService.addSavequestion(question1);
        if(result==true){
            responseData.setMsg("添加面试题成功");
        }else{
            responseData.setMsg("添加失败");
        }

        return responseData;

    }

    /**
     * 分页查询所有数据
     * @param session
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/queryquestionbyno.do")
    @ResponseBody
    public ResponseData queryQuestionByNo(HttpSession session,Integer page,Integer limit){

        User user = (User) session.getAttribute("user");
        String uno = user.getNo();

        List<Question> questions = resourceService.selectQuestion(uno, page, limit);
        PageInfo<Question> questionPageInfo = new PageInfo<>(questions);
        ResponseData<Object> responseData = new ResponseData<>();

        responseData.setData(0);
        responseData.setCount((int) questionPageInfo.getTotal());
        responseData.setData(questions);
        return responseData;
    }

    /**
     * 删除资源库
     * @param id
     * @return
     */
    @RequestMapping("/questiondelete.do")
    @ResponseBody
    public ResponseData questionDelete(int id){
        ResponseData<Object> responseData = new ResponseData<>();


        boolean result = resourceService.deleteQuestion(id);
        if(result==true){
            responseData.setCode(1);
            responseData.setMsg("删除成功");
        }else{
            responseData.setCode(0);
            responseData.setMsg("删除失败");
        }
        return responseData;
    }

    /**
     * 修改资源
     * @param question
     * @return
     */
    @RequestMapping("/questionupdate.do")
    @ResponseBody
    public ResponseData questionUpdate(Question question){

        ResponseData<Object> responseData = new ResponseData<>();

        boolean result = resourceService.updateQuestion(question);

        if(result==true){
            responseData.setCode(1);
            responseData.setMsg("更新成功");
        }else{
            responseData.setCode(0);
            responseData.setMsg("更新失败");
        }
        return responseData;
    }
}
